<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/xhtml;charset=UTF-8"/>
<title>Schedulatore EDF:  File sorgente trunk/Provaposix2.h</title>
<link href="tabs.css" rel="stylesheet" type="text/css"/>
<link href="search/search.css" rel="stylesheet" type="text/css"/>
<script type="text/javaScript" src="search/search.js"></script>
<link href="doxygen.css" rel="stylesheet" type="text/css"/>
</head>
<body onload='searchBox.OnSelectItem(0);'>
<!-- Generato da Doxygen 1.7.1 -->
<script type="text/javascript"><!--
var searchBox = new SearchBox("searchBox", "search",false,'Cerca');
--></script>
<div class="navigation" id="top">
  <div class="tabs">
    <ul class="tablist">
      <li><a href="index.html"><span>Pagina&nbsp;Principale</span></a></li>
      <li><a href="annotated.html"><span>Classi</span></a></li>
      <li class="current"><a href="files.html"><span>File</span></a></li>
      <li id="searchli">
        <div id="MSearchBox" class="MSearchBoxInactive">
        <span class="left">
          <img id="MSearchSelect" src="search/mag_sel.png"
               onmouseover="return searchBox.OnSearchSelectShow()"
               onmouseout="return searchBox.OnSearchSelectHide()"
               alt=""/>
          <input type="text" id="MSearchField" value="Cerca" accesskey="S"
               onfocus="searchBox.OnSearchFieldFocus(true)" 
               onblur="searchBox.OnSearchFieldFocus(false)" 
               onkeyup="searchBox.OnSearchFieldChange(event)"/>
          </span><span class="right">
            <a id="MSearchClose" href="javascript:searchBox.CloseResultsWindow()"><img id="MSearchCloseImg" border="0" src="search/close.png" alt=""/></a>
          </span>
        </div>
      </li>
    </ul>
  </div>
  <div class="tabs2">
    <ul class="tablist">
      <li><a href="files.html"><span>Elenco&nbsp;dei&nbsp;file</span></a></li>
      <li><a href="globals.html"><span>Membri&nbsp;dei&nbsp;file</span></a></li>
    </ul>
  </div>
<div class="header">
  <div class="headertitle">
<h1>trunk/Provaposix2.h</h1>  </div>
</div>
<div class="contents">
<a href="_provaposix2_8h.html">Vai alla documentazione di questo file.</a><div class="fragment"><pre class="fragment"><a name="l00001"></a>00001 <span class="preprocessor">#include &lt;stdio.h&gt;</span>
<a name="l00002"></a>00002 <span class="preprocessor">#include &lt;time.h&gt;</span>
<a name="l00003"></a>00003 <span class="preprocessor">#include &lt;pthread.h&gt;</span>
<a name="l00004"></a>00004 
<a name="l00005"></a>00005 <span class="comment">/* For safe condition variable usage, must use a boolean predicate and  */</span>
<a name="l00006"></a>00006 <span class="comment">/* a mutex with the condition.                                          */</span>
<a name="l00007"></a><a class="code" href="_provaposix2_8h.html#aca32ac4ee49dc9222e33c677b9af1c0d">00007</a> <span class="keywordtype">int</span>                 <a class="code" href="_provaposix2_8h.html#aca32ac4ee49dc9222e33c677b9af1c0d">workToDo</a> = 0;
<a name="l00008"></a><a class="code" href="_provaposix2_8h.html#a0a1433271fddfed84bc959ae6c202e5a">00008</a> pthread_cond_t      <a class="code" href="_provaposix2_8h.html#a0a1433271fddfed84bc959ae6c202e5a">cond</a>  = PTHREAD_COND_INITIALIZER;
<a name="l00009"></a><a class="code" href="_provaposix2_8h.html#a4acff8232e4aec9cd5c6dc200ac55ef3">00009</a> pthread_mutex_t     <a class="code" href="_provaposix2_8h.html#a4acff8232e4aec9cd5c6dc200ac55ef3">mutex</a> = PTHREAD_MUTEX_INITIALIZER;
<a name="l00010"></a>00010 
<a name="l00011"></a><a class="code" href="_provaposix2_8h.html#a51ca9914034cd3e92903476fd4eebbd6">00011</a> <span class="preprocessor">#define NTHREADS                3</span>
<a name="l00012"></a><a class="code" href="_provaposix2_8h.html#af4110b4ec050031340be248ca32bc418">00012</a> <span class="preprocessor"></span><span class="preprocessor">#define WAIT_TIME_SECONDS       5</span>
<a name="l00013"></a>00013 <span class="preprocessor"></span>
<a name="l00014"></a><a class="code" href="_provaposix2_8h.html#a3808f97acf9da467105f7a100c862ea3">00014</a> <span class="keywordtype">void</span> *<a class="code" href="_provaposix2_8h.html#a3808f97acf9da467105f7a100c862ea3">threadfunc</a>(<span class="keywordtype">void</span> *parm)
<a name="l00015"></a>00015 {
<a name="l00016"></a>00016   <span class="keywordtype">int</span>               rc;
<a name="l00017"></a>00017   time_t tt_now;
<a name="l00018"></a>00018   timespec   ts;
<a name="l00019"></a>00019 
<a name="l00020"></a>00020   rc = pthread_mutex_lock(&amp;<a class="code" href="_provaposix2_8h.html#a4acff8232e4aec9cd5c6dc200ac55ef3">mutex</a>);
<a name="l00021"></a>00021 
<a name="l00022"></a>00022   <span class="comment">/* Usually worker threads will loop on these operations */</span>
<a name="l00023"></a>00023   <span class="keywordflow">while</span> (1) {
<a name="l00024"></a>00024     tt_now = time(NULL);
<a name="l00025"></a>00025     <span class="comment">/* Convert from timeval to timespec */</span>
<a name="l00026"></a>00026         ts.tv_sec  = (long)tt_now + <a class="code" href="_provaposix2_8h.html#af4110b4ec050031340be248ca32bc418">WAIT_TIME_SECONDS</a>;
<a name="l00027"></a>00027     ts.tv_nsec = 0;
<a name="l00028"></a>00028 
<a name="l00029"></a>00029     <span class="keywordflow">while</span> (!<a class="code" href="_provaposix2_8h.html#aca32ac4ee49dc9222e33c677b9af1c0d">workToDo</a>) {
<a name="l00030"></a>00030       printf(<span class="stringliteral">&quot;Thread blocked\n&quot;</span>);
<a name="l00031"></a>00031       rc = pthread_cond_timedwait(&amp;<a class="code" href="_provaposix2_8h.html#a0a1433271fddfed84bc959ae6c202e5a">cond</a>, &amp;<a class="code" href="_provaposix2_8h.html#a4acff8232e4aec9cd5c6dc200ac55ef3">mutex</a>, &amp;ts);
<a name="l00032"></a>00032       <span class="comment">/* If the wait timed out, in this example, the work is complete, and   */</span>
<a name="l00033"></a>00033       <span class="comment">/* the thread will end.                                                */</span>
<a name="l00034"></a>00034       <span class="comment">/* In reality, a timeout must be accompanied by some sort of checking  */</span>
<a name="l00035"></a>00035       <span class="comment">/* to see if the work is REALLY all complete. In the simple example    */</span>
<a name="l00036"></a>00036       <span class="comment">/* we&#39;ll just go belly up when we time out.                            */</span>
<a name="l00037"></a>00037       <span class="keywordflow">if</span> (rc == ETIMEDOUT) {
<a name="l00038"></a>00038         printf(<span class="stringliteral">&quot;Wait timed out!\n&quot;</span>);
<a name="l00039"></a>00039         rc = pthread_mutex_unlock(&amp;<a class="code" href="_provaposix2_8h.html#a4acff8232e4aec9cd5c6dc200ac55ef3">mutex</a>);
<a name="l00040"></a>00040         pthread_exit(NULL);
<a name="l00041"></a>00041       }
<a name="l00042"></a>00042     }
<a name="l00043"></a>00043 
<a name="l00044"></a>00044     printf(<span class="stringliteral">&quot;Thread consumes work here\n&quot;</span>);
<a name="l00045"></a>00045     <a class="code" href="_provaposix2_8h.html#aca32ac4ee49dc9222e33c677b9af1c0d">workToDo</a> = 0;
<a name="l00046"></a>00046   }
<a name="l00047"></a>00047 
<a name="l00048"></a>00048   rc = pthread_mutex_unlock(&amp;<a class="code" href="_provaposix2_8h.html#a4acff8232e4aec9cd5c6dc200ac55ef3">mutex</a>);
<a name="l00049"></a>00049   <span class="keywordflow">return</span> NULL;
<a name="l00050"></a>00050 }
</pre></div></div>
</div>
<!--- window showing the filter options -->
<div id="MSearchSelectWindow"
     onmouseover="return searchBox.OnSearchSelectShow()"
     onmouseout="return searchBox.OnSearchSelectHide()"
     onkeydown="return searchBox.OnSearchSelectKey(event)">
<a class="SelectItem" href="javascript:void(0)" onclick="searchBox.OnSelectItem(0)"><span class="SelectionMark">&nbsp;</span>Tutto</a><a class="SelectItem" href="javascript:void(0)" onclick="searchBox.OnSelectItem(1)"><span class="SelectionMark">&nbsp;</span>Classi</a><a class="SelectItem" href="javascript:void(0)" onclick="searchBox.OnSelectItem(2)"><span class="SelectionMark">&nbsp;</span>File</a><a class="SelectItem" href="javascript:void(0)" onclick="searchBox.OnSelectItem(3)"><span class="SelectionMark">&nbsp;</span>Funzioni</a><a class="SelectItem" href="javascript:void(0)" onclick="searchBox.OnSelectItem(4)"><span class="SelectionMark">&nbsp;</span>Variabili</a><a class="SelectItem" href="javascript:void(0)" onclick="searchBox.OnSelectItem(5)"><span class="SelectionMark">&nbsp;</span>Ridefinizioni di tipo (typedef)</a><a class="SelectItem" href="javascript:void(0)" onclick="searchBox.OnSelectItem(6)"><span class="SelectionMark">&nbsp;</span>Definizioni</a></div>

<!-- iframe showing the search results (closed by default) -->
<div id="MSearchResultsWindow">
<iframe src="" frameborder="0" 
        name="MSearchResults" id="MSearchResults">
</iframe>
</div>

<hr class="footer"/><address class="footer"><small>Generato il Fri Sep 10 2010 15:26:14 per Schedulatore EDF da&nbsp;
<a href="http://www.doxygen.org/index.html">
<img class="footer" src="doxygen.png" alt="doxygen"/></a> 1.7.1 </small></address>
</body>
</html>
